package com.myrobot;

import java.awt.Point;
import java.util.HashMap;
import java.util.Map;

import com.myrobot.model.PointModel;
import com.myrobot.ui.PointFrame;
import com.myrobot.ui.PointFrame.OnActionPointListener;
import com.myrobot.util.FileUtil;

public class MyRobot {
	static String sTargets[] = null;
	static String sOutput = null;
	
	public static void usage() {
		System.out.println("MyRobot [option] [param]");
		System.out.println("options:");
		System.out.println("--point(-p):get target point");
		System.out.println("--targets(-t):set target names splite with \",\"");
		System.out.println("--output(-o):output file");
	}

	public static void getPoint(Map<String, String> options) {
		
		if (options.containsKey("--targets")) {
			sTargets = options.get("--targets").split(",");
		} else if (options.containsKey("-t")) {
			sTargets = options.get("-t").split(",");
		}
		if (options.containsKey("--output")) {
			sOutput = options.get("--output");
		} else if (options.containsKey("-o")) {
			sOutput = options.get("-o");
		}

		PointFrame pointFrame = new PointFrame(sTargets);
		pointFrame.setActionPointListener(new OnActionPointListener() {

			@Override
			public void finish(Map<String, Point> actionPoints) {
				// TODO Auto-generated method stub
				PointModel mode = new PointModel(actionPoints);
				String data = mode.toString();
				if(sOutput == null) {
					System.out.println(data);
				}else {
					FileUtil.writeFile(sOutput, data);
				}
			}

			@Override
			public void cancel() {
				// TODO Auto-generated method stub

			}
		});
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<String, String> options = new HashMap<>();
		String key = "";
		String value = "";
		for (String arg : args) {
			if (arg.startsWith("-")) {
				key = arg;
				options.put(key, "");
			} else {
				value = arg;
				options.put(key, value);
			}
		}

		for (String o : options.keySet()) {
			if (o.equals("--point") || o.equals("-p")) {
				getPoint(options);
				return;
			}
		}
		usage();
	}

}
